package com.starlight.easybuyprictice.util;

import com.starlight.easybuyprictice.service.Result;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author yfy2001
 * @date 2022/11/28 21:18
 */
public class WebUtil {
    public static void showInterceptionInfo(HttpServletResponse response,String msg){
        showInterceptionInfo(response,0, msg);
    }
    public static void showInterceptionInfo(HttpServletResponse response,int status,String msg){
        showInterceptionInfo(response,status,msg,null);
    }
    public static void showInterceptionInfo(HttpServletResponse response,int status,String msg,Object data) {
        Result result = new Result();
        result.setStatus(status);
        result.setMsg(msg);
        result.setData(data);
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8" );
            response.setHeader("Content-Type" ,
                    "application/json ; charset=UTF-8" );
            out = response.getWriter();
            out.write(JSONObject.fromObject(result).toString());
        }catch (IOException ie) {
            ie.printStackTrace();
        }
        if (out != null) {
            out.close();
        }
    }
}
